package com.xiaoma.myaudience.biz.task;

import org.json.JSONArray;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.text.TextUtils;

import com.xiaoma.myaudience.biz.model.CategorySearchUrlInfo;
import com.xiaoma.myaudience.biz.model.CategoryVideoInfo;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;


public class CategoryInfoTask extends BaseDataAsyncTask<CategorySearchUrlInfo, Void, String> {
    private final static String TAG = "CategoryInfoTask";
    public final static int CATEGORY_ORDER = 1;
    public final static int CATEGORY_PAGE_START = 0;
    public final static int CATEGORY_PAGE_LENGTH = 25;

    /**
     * {@inheritDoc}
     * 
     * @param context
     * @param taskType
     * @param callback
     */
    public CategoryInfoTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);
    }

    @Override
    protected String doInBackground(CategorySearchUrlInfo... params) {
    	String category = null;
        if(params[0] == null) return category;
        String url = params[0].getCategorySearchUrl();
        Logger.i("url is "+url);

        if (!TextUtils.isEmpty(url)) {
            try {
                // 获取热播信息
                JSONArray jsonArray = HttpUtils.getHttpJSONArrayResult(getHttpClient(), url, null,
                        HttpUtils.GET, mContext);
                if (jsonArray != null && jsonArray.length() > 0) {                   	
                    CategoryVideoInfo info;
                    final int size = jsonArray.length();
                    ContentValues[] values = new ContentValues[size];
                    for (int i = 0; i < size; i++) {
                        info = new CategoryVideoInfo();
                        info.readFromJSONObject(jsonArray.getJSONObject(i));
                        values[i] = getContentValues(info, params[0].getType());
                    }
                    final ContentResolver resolver = mContext.getContentResolver();
//                    resolver.bulkInsert(CategorySearchItem.getUri(), values);  
                    category = params[0].getType();
                } else if(jsonArray.length() == 0) return "";
                
            } catch (Exception e) {
                Logger.e(TAG, "doInBackground error!!!", e);
            } finally {
                closeHttpClient();
            }
        }
        return category;
    }
    
    private ContentValues getContentValues(CategoryVideoInfo info, String category) {
        
        final ContentValues values = new ContentValues();
//        values.put(CategorySearchItem.PARAM_PID, info.mPid);
//        values.put(CategorySearchItem.PARAM_DIGEST, info.mDigest);
//        values.put(CategorySearchItem.PARAM_DIRECTOR, info.mDirector);
//        values.put(CategorySearchItem.PARAM_IMG, info.mImgUrlPath);
//        values.put(CategorySearchItem.PARAM_LAUNCH, info.mLaunch);
//        values.put(CategorySearchItem.PARAM_PERFORMER, info.mPerformer);
//        values.put(CategorySearchItem.PARAM_RATING, info.mRating);
//        values.put(CategorySearchItem.PARAM_TITLE, info.mTitle);
//        values.put(CategorySearchItem.PARAM_CATEGORY, category);
//        values.put(CategorySearchItem.PARAM_TYPE, info.mType);

        return values;      
    }

}
